package com.monlyu.util;

import java.io.File;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/********************************************************************************
 * <b>功能描述☞</b>数组的操作集合<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.util.ArrayUtil.java<br>
 * <b>开发日期☞</b>2010-3-7 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 ********************************************************************************/
public class ArrayUtil {

    /**
     * 字符串数组转化为List
     */
    public static List<String> toList(String[] array) {
        List<String> list = Lists.newArrayList();
        if (array != null) {
            for (String string : array) {
                list.add(string);
            }
        }
        return list;
    }

    /**
     * 把字符串转化为Set集合
     */
    public static Set<String> toSet(String[] array) {
        Set<String> sets = Sets.newHashSet();
        if (array != null) {
            for (String string : array) {
                sets.add(string);
            }
        }
        return sets;
    }

    /**
     * 把字符串数组变为long行
     */
    public static List<Long> str2Long(String[] split) {
        List<Long> all = Lists.newArrayList();
        for (String string : split) {
            try {
                Long l = Long.parseLong(string);
                all.add(l);
            } catch (Exception e) {
                continue;
            }
        }
        return all;
    }

    public static int indexOf(Object all[], Object toFind) {
        for (int i = 0; i < all.length; i++) {
            if (all[i].equals(toFind)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 字符串的合并
     * 
     * @param joiner
     *            合并者
     * @param paths
     *            数组
     * @return
     */
    public static String join(String joiner, String... paths) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paths.length; i++) {
            String string = paths[i];
            sb.append(string);
            if (i != paths.length - 1) {
                sb.append(joiner);
            }
        }
        return sb.toString();
    }

    public static String pathJoin(String... paths) {
        return join(File.separator, paths);
    }

    public static void main(String[] args) {
        System.out.println(ArrayUtil.pathJoin("AAA", "BBB", "CCC", "DDD"));
    }
}
